package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同表管理Controller
 * 
 * @author zhangjun
 * @date 2020-02-13
 */
@Controller
@RequestMapping("/system/contractDetail")
public class AlpContractDetailController extends BaseController
{
    private String prefix = "system/contractDetail";

    @Autowired
    private IAlpContractDetailService alpContractDetailService;

    @Autowired
    private IAlpContractStageService alpContractStageService;

    @Autowired
    private IAlpContractStageDetailService alpContractStageDetailService;



    @Autowired
    private IAlpYingshouService alpYingshouService;

    @RequiresPermissions("system:contractDetail:view")
    @GetMapping("/new")
    public String contractDetailNew()
    {
        return prefix + "/contractDetailNew";
    }

    @RequiresPermissions("system:contractDetail:view")
    @GetMapping()
    public String contractDetail()
    {
        return prefix + "/contractDetail";
    }

    /**
     * 查询合同表管理列表
     */
    @RequiresPermissions("system:contractDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpContractDetail alpContractDetail)
    {
        startPage();
        List<AlpContractDetail> list = alpContractDetailService.selectAlpContractDetailList(alpContractDetail);

        return getDataTable(list);
    }

    /**
     * 导出合同表管理列表
     */
    @RequiresPermissions("system:contractDetail:export")
    @Log(title = "合同表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpContractDetail alpContractDetail)
    {
        List<AlpContractDetail> list = alpContractDetailService.selectAlpContractDetailList(alpContractDetail);
        ExcelUtil<AlpContractDetail> util = new ExcelUtil<AlpContractDetail>(AlpContractDetail.class);
        return util.exportExcel(list, "contractDetail");
    }

    /**
     * 导出合同表管理列表
     */
    @RequiresPermissions("system:contractDetail:export")
    @Log(title = "应收账款", businessType = BusinessType.EXPORT)
    @PostMapping("/exportYingShou")
    @ResponseBody
    public AjaxResult exportYingShou(AlpContractDetail alpContractDetail)
    {
        List<AlpYingshou> yingShouList = new ArrayList<AlpYingshou>();
        List<AlpContractDetail> list = alpContractDetailService.selectAlpContractDetailList(alpContractDetail);
        for (AlpContractDetail contractDetail : list) {
            //根据合同id 查出本合同的阶段表内容
            List<AlpContractStage> contractStages = alpContractStageService.selectAlpContractStageByContractId(contractDetail.getContractId());
            for (AlpContractStage contractStage : contractStages) {
                AlpYingshou yingshou = new AlpYingshou();
                //根据阶段id 查出阶段最新的预测预算实际
                List<AlpContractStageDetail> stageDetails = alpContractStageDetailService.selectAlpContractStageDetailByStageId(contractStage.getStageId());

                contractStage.setAlpContractStageDetails(stageDetails);

//                yingshou.setDepartment(contractDetail);
                yingshou.setStageName(contractStage.getStageName());
                yingshou.setSalesmanFirst(contractStage.getStagePrincipal1());
                yingshou.setSalesmanSecond(contractStage.getStagePrincipal2());
                yingshou.setCollectionCompany(contractDetail.getBiddingCompany());//逻辑错误
                yingshou.setCustomerName(contractDetail.getPartyAName());
                yingshou.setContractNo(contractDetail.getContractNo());
                yingshou.setContractName(contractDetail.getContractName());
                yingshou.setContractMoney(contractDetail.getBiddingMoney());

                yingShouList.add(yingshou);
            }

        }

        ExcelUtil<AlpYingshou> util = new ExcelUtil<AlpYingshou>(AlpYingshou.class);
        return util.exportExcel(yingShouList, "应收表");
    }


    /**
     * 新增合同表管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存合同表管理
     */
    @RequiresPermissions("system:contractDetail:add")
    @Log(title = "合同表管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpContractDetail alpContractDetail)
    {

        return toAjax(alpContractDetailService.insertAlpContractDetail(alpContractDetail));
    }

    /**
     * 修改合同表管理
     */
    @GetMapping("/edit/{contractId}")
    public String edit(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        AlpContractDetail alpContractDetail = alpContractDetailService.selectAlpContractDetailById(contractId);
        alpContractDetail.setUserId(ShiroUtils.getSysUser().getUserId());
        mmap.put("alpContractDetail", alpContractDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存合同表管理
     */
    @RequiresPermissions("system:contractDetail:edit")
    @Log(title = "合同表管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpContractDetail alpContractDetail)
    {
        alpContractDetail.setUserId(ShiroUtils.getSysUser().getUserId());
        return toAjax(alpContractDetailService.updateAlpContractDetail(alpContractDetail));
    }

    /**
     * 删除合同表管理
     */
    @RequiresPermissions("system:contractDetail:remove")
    @Log(title = "合同表管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpContractDetailService.deleteAlpContractDetailByIds(ids));
    }

    /**
     * 可编辑表格--管理合同预算、预测、实际回款金额
     */
    @GetMapping("/moneyBack/{contractId}")
    public String editTableBudget(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        AlpContractDetail alpContractDetail = alpContractDetailService.selectAlpContractDetailById(contractId);
        mmap.put("alpContractDetail", alpContractDetail);
        List<AlpContractStage> list = alpContractStageService.selectAlpContractStageByContractId(contractId);
        mmap.put("stageList", list);
        return prefix + "/moneyBack";
    }

}
